#include <graphics.h>
#include <math.h>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm> // 添加头文件支持 sort 函数
#include <ege/sys_edit.h>

using namespace std;

int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
string in(const string& s, const string& ss) {
	initgraph(400, 300);
	movewindow((screenWidth - 400 ) / 2, (screenHeight - 300) / 2, true);
	char str[100] = "";
	inputbox_getline(s.c_str(), ss.c_str(), str, sizeof(str) / sizeof(*str));
	// 将字符数组转换为字符串
	string result(str);
	return result;
}
// 菜品结构体
struct Dish {
	string name;
	int time;
};
// 解析输入格式："菠菜 2"
bool parse_input(const string& line, string& name, int& time) {
	istringstream iss(line);
	return static_cast<bool>(iss >> name >> time);
}

void bg(){
	
	PIMAGE img = newimage();
	getimage(img, "慕慕.jpg");
	putimage(0, 0, 400, 300, img, 0, 0, getwidth(img), getheight(img));
	
	delimage(img);
}
int main()
{
	initgraph(640, 480);
	setcolor(RGB(0xff, 0xff, 0xff));
	setfillcolor(RGB(0, 0, 0xff));
	setrendermode(RENDER_MANUAL);
	
	setbkmode(TRANSPARENT);
	
	// 使用 vector 动态存储菜品
	vector<Dish> dishes;
	
	for (; is_run(); delay_fps(60))
	{
		setcaption("火候大师 - 计算做饭营养最佳时间");
		string t = "添加数据";
		int N = 12;
		
		int total_time = atoi(in(t, "请输入总烹饪时长(min):").c_str());
		
		for (int i = 0; i < N; i++) {
			char prompt[30];
			snprintf(prompt, sizeof(prompt), "输入第%d种菜的名称和烹饪时长：", (int)dishes.size() + 1);
			string input = in(t, prompt);
			if (input == "2") break;
			
			string name;
			int time;
			if (!parse_input(input, name, time)) {
				cleardevice();
				outtextxy(10, 100, "格式错误，请按“菜名 时间”输入（例如：西红柿 5）");
				delay_ms(2000);
				continue;
			}
			
			dishes.push_back({ name, time });
		}
		
		
		
		// 显示所有菜品
		cleardevice();
		bg();
		outtextxy(50, 50, "您输入的菜品如下：");
		int y = 100;
		for (size_t i = 0; i < dishes.size(); ++i) {
			char buffer[100];
			snprintf(buffer, sizeof(buffer), "%zu. %s %d", i + 1, dishes[i].name.c_str(), dishes[i].time);
			outtextxy(50, y, buffer);
			y += 30;
		}
		
		outtextxy(50, y + 20, "按任意键继续...");
		while (!kbhit()) delay_ms(10);
		getch();
		// 对菜品按时间排序
		sort(dishes.begin(), dishes.end(), [](const Dish& a, const Dish& b) {
			return a.time > b.time;
		});
		// === 新增：流程图显示页 ===
		cleardevice();
		bg();
		setcolor(WHITE);
		outtextxy(50, 50, "流程安排如下：");
		y = 100;
		for (size_t i = 0; i < dishes.size(); ++i) {
			char buffer[100];
			snprintf(buffer, sizeof(buffer), "%zu. %d min 后, 放入 %s", i + 1, total_time-dishes[i].time, dishes[i].name.c_str());
			
			outtextxy(50, y, buffer);
			y += 30;
		}
		while (!kbhit()) delay_ms(10);
		getch();
		
		break; // 结束循环，结束程序
	}
	
	closegraph();
	return 0;
}
